package com.huirui.server.controller.mini.program;

import com.huirui.server.pojo.dto.OrderCreateDto;
import com.huirui.server.pojo.dto.OrderPrepareInfoDTO;
import com.huirui.server.pojo.dto.OrderSubmitDto;
import com.huirui.server.pojo.result.Result;
import com.huirui.server.pojo.vo.*;
import com.huirui.server.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/order")
@Slf4j
@Validated
@Tag(name = "订单管理", description = "微信小程序订单管理相关")
public class OrderController {
    @Resource
    private OrderService orderService;


    @GetMapping("/confirm")
    @Operation(summary = "确认订单")
    public Result<List<OrderConfirmVo>> listConfirmProductInfo(@RequestParam("ids") List<Long> ids) {
        List<OrderConfirmVo> res = orderService.listConfirmProductInfo(ids);
        return Result.success(res);
    }

    @PostMapping
    @Operation(summary = "创建新订单")
    public Result<OrderResponseVo> createOrder(@Valid @RequestBody OrderCreateDto orderCreateDto) {
        OrderResponseVo orderResponseVo = orderService.createOrder(orderCreateDto);
        return Result.success(orderResponseVo);
    }

    @GetMapping("/all")
    @Operation(summary = "获取所有的订单")
    public Result<List<OrderResponseVo>> getAllOrderList(
            @Min(value = 1, message = "页码必须大于等于1")
            @RequestParam Long page,
            @Min(value = 1, message = "每页条数必须大于等于1")
            @RequestParam Long pageSize
    ) {
        List<OrderResponseVo> res = orderService.getAllOrderList(page, pageSize);
        return Result.success(res);
    }


    @GetMapping("/pending/payment")
    @Operation(summary = "获取待付款的订单")
    public Result<List<OrderResponseVo>> getPendingPaymentOrderList(
            @Min(value = 1, message = "页码必须大于等于1")
            @RequestParam Long page,
            @Min(value = 1, message = "每页条数必须大于等于1")
            @RequestParam Long pageSize
    ) {
        List<OrderResponseVo> res = orderService.getPendingPaymentOrderList(page, pageSize);
        return Result.success(res);
    }

    @GetMapping("/pending/receipt")
    @Operation(summary = "获取待收货的订单")
    public Result<List<OrderResponseVo>> getPendingReceiptOrderList(
            @Min(value = 1, message = "页码必须大于等于1")
            @RequestParam Long page,
            @Min(value = 1, message = "每页条数必须大于等于1")
            @RequestParam Long pageSize
    ) {
        List<OrderResponseVo> res = orderService.getPendingReceiptOrderList(page, pageSize);
        return Result.success(res);
    }

    @GetMapping("/finished")
    @Operation(summary = "获取已完成的订单")
    public Result<List<OrderResponseVo>> getFinishedOrderList(
            @Min(value = 1, message = "页码必须大于等于1")
            @RequestParam Long page,
            @Min(value = 1, message = "每页条数必须大于等于1")
            @RequestParam Long pageSize
    ) {
        List<OrderResponseVo> res = orderService.getFinishedOrderList(page, pageSize);
        return Result.success(res);
    }

    @GetMapping("/after/sales")
    @Operation(summary = "获取售后中的订单")
    public Result<List<OrderResponseVo>> getAfterSalesOrderList(
            @Min(value = 1, message = "页码必须大于等于1")
            @RequestParam Long page,
            @Min(value = 1, message = "每页条数必须大于等于1")
            @RequestParam Long pageSize
    ) {
        List<OrderResponseVo> res = orderService.getAfterSalesOrderList(page, pageSize);
        return Result.success(res);
    }

    @GetMapping("/pending/delivery")
    @Operation(summary = "获取待发货的订单")
    public Result<List<OrderResponseVo>> getPendingDeliveryOrderList(
            @Min(value = 1, message = "页码必须大于等于1")
            @RequestParam Long page,
            @Min(value = 1, message = "每页条数必须大于等于1")
            @RequestParam Long pageSize
    ) {
        List<OrderResponseVo> res = orderService.getPendingDeliveryOrderList(page, pageSize);
        return Result.success(res);
    }

    @DeleteMapping("/cancel/{id}")
    @Operation(summary = "取消订单")
    public Result<Void> cancelOrder(@PathVariable("id") Long orderId) {
        orderService.cancelOrder(orderId);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除订单")
    public Result<Void> deleteOrder(@PathVariable("id") Long orderId) {
        orderService.deleteOrder(orderId);
        return Result.success();
    }


    @GetMapping("/detail")
    @Operation(summary = "获取订单详情")
    public Result<OrderDetailVo> getOrderDetail(@RequestParam("orderId") Long orderId) {
        OrderDetailVo orderDetailVo = orderService.getOrderDetail(orderId);
        return Result.success(orderDetailVo);
    }

    @PutMapping("/address")
    @Operation(summary = "修改订单收货地址")
    public Result<Void> updateOrderDeliveryAddress(
            @RequestParam("deliveryAddressId") Long id,
            @RequestParam("orderId") Long orderId) {
        orderService.updateOrderDeliveryAddress(id, orderId);
        return Result.success();
    }

    @GetMapping("/search")
    @Operation(summary = "根据订单号或商品名称查询订单")
    public Result<OrderResponseVo> searchOrder(@RequestParam("keyword") String keyword) {
        return null;
    }


    @PostMapping("/prepare/info")
    @Operation(summary = "获取订单预支付信息")
    public Result<OrderPrepareInfoVo> getOrderPrepareInfo(@RequestBody OrderPrepareInfoDTO orderPrepareInfoDTO) {
        OrderPrepareInfoVo orderPrepareInfoVo = orderService.getOrderPrepareInfo(orderPrepareInfoDTO);
        return Result.success(orderPrepareInfoVo);
    }

    @PostMapping("/submit")
    @Operation(summary = "提交订单")
    public Result<OrderSubmitVo> submitOrder(@RequestParam @Validated OrderSubmitDto orderSubmitDto) {
        OrderSubmitVo orderSubmitVo = orderService.submitOrder(orderSubmitDto);
        return Result.success(orderSubmitVo);
    }


}
